home *** CD-ROM | disk | FTP | other *** search
- Path: news.crystalball.com!news
- From: Larry Weiss <lfw@oc.com>
- Newsgroups: comp.lang.c
- Subject: Re: Help : Printf() format %E !
- Date: Thu, 22 Feb 1996 08:06:39 -0600
- Organization: crystalball.com
- Message-ID: <312C786F.6696@oc.com>
- References: <4g18nf$jps@ias2.ichange.com> <danpop.824495447@rscernix>
- NNTP-Posting-Host: external.oc.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Dan Pop wrote:
- >
- > In <4g18nf$jps@ias2.ichange.com> akmp@mindware.soft.net (anil kumar m p) writes:
- > >i am involved converting programs written in Fortran onto C.
- > >one problem that i am unable to solve is printing exponential
- > >values in C.
- > >Fortran prints exponential values with 0.4567E02
- > >whereas C prints the same as 4.5670E01.
- > >How do i make C print in 0.somethingEsomething format.
-
-
- > The %E format cannot do what you need, by definition. So, you have to
- > split the number into mantissa and exponent and printf them separately.
- > You can do this either mathematically or by playing with stdio functions:
- > double mantissa;
- > int exponent;
- > sprintf(buff, "%E", 0.4567E02);
- > *strchr(buff, 'E') = ' ';
- > sscanf(buff, "%lf %d", &mantissa, &exponent);
- > mantissa /= 10;
- > if (mantissa != 0) exponent++;
- > else exponent = 0;
- > printf("%fE%+.02d\n", mantissa, exponent);
- >
- > If mathematical functions are inexpensive on your system, splitting
- > the number into mantissa and exponent with log10 and pow might be
- > more efficient.
-
- Or, use sprintf() to get a string containing the components, and
- then use character manipulations to adjust the formatting.
-